﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.Forms.Rules;
using RibbonEditor.Rules;
using RibbonEditor.AppCode;

namespace RibbonEditor
{
    public class TabDisplayRules: IXmlBuildable, IChildDeletable
    {
        public List<TabDisplayRule> Rules;

        public TabDisplayRules()
        {
            this.Rules = new List<TabDisplayRule>();
        }

        public TabDisplayRules(XmlNode tdRuleXmlNode) : this()
        {
            try
            {
                foreach (XmlNode node in tdRuleXmlNode.ChildNodes)
                {
                    TabDisplayRule tdRule = new TabDisplayRule(node);
                    this.Rules.Add(tdRule);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode ruleDefinitionsXmlNode)
        {
            try
            {
                XmlNode tabDisplayRulesNode = XmlHelper.AddNode(ruleDefinitionsXmlNode, "TabDisplayRules");

                foreach (TabDisplayRule tdRule in this.Rules)
                {
                    tdRule.AddNode(tabDisplayRulesNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        internal void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode tabDisplayRulesNode = new TreeNode("TabDisplayRules");
                tabDisplayRulesNode.Tag = this;
                tabDisplayRulesNode.ContextMenuStrip = this.BuildRulesMenuStrip(tabDisplayRulesNode);

                foreach (TabDisplayRule rule in this.Rules)
                {
                    rule.BuildTreeViewElement(tabDisplayRulesNode);
                }

                parentNode.Nodes.Add(tabDisplayRulesNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildRulesMenuStrip(TreeNode relatedNode)
        {
            ContextMenuStrip cms = new ContextMenuStrip();
            cms.Tag = relatedNode;

            ToolStripMenuItem tsmi = new ToolStripMenuItem("Add Rule");
            tsmi.Click += new EventHandler(tsmi_Click);
            cms.Items.Add(tsmi);

            return cms;
        }

        void tsmi_Click(object sender, EventArgs e)
        {
            try
            {
                TreeNode selectedNode = (TreeNode)((ToolStripItem)sender).GetCurrentParent().Tag;

                RuleSelectionDialog rsDialog = new RuleSelectionDialog(RuleSelectionDialog.RuleType.TabDisplayRule);
                rsDialog.StartPosition = FormStartPosition.CenterParent;

                if (rsDialog.ShowDialog() == DialogResult.OK)
                {
                    ITabDisplayRule selectedRule = (ITabDisplayRule)rsDialog.SelectedRule;

                    // Construction de l'élément parent à la règle sélectionnée (TabDisplayRule)
                    TabDisplayRule dRule = new TabDisplayRule();
                    dRule.Rules.Add(selectedRule);

                    this.Rules.Add(dRule);
                    dRule.BuildTreeViewElement(selectedNode);
                }

                selectedNode.Expand();
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is TabDisplayRule)
                {
                    this.Rules.Remove((TabDisplayRule)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
